﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CustomsSystem.BLL;
using System.Data;
using Maticsoft.Common;

namespace WEB.template
{
    public partial class feeTemplateDesign : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                Bind();
                //显示货物类别
                ddlGoodsName.DataSource = GetGoodsType();
                ddlGoodsName.DataTextField = "codeName";
                ddlGoodsName.DataValueField = "codeID";
                ddlGoodsName.DataBind();

                if (Session["feetemplet_ID"] != null && Session["feetemplet_ID"].ToString() != "")
                { ReBind();
                lbTID.Text = "模板编号：" + Session["feetemplet_ID"].ToString ();
                }
               

            }
        }
        /// <summary>
        /// 初始绑定——默认5行空白数据
        /// </summary>
        public void Bind()
        {

            DataTable dt1 = new DataTable("Table1");
            dt1.Columns.Add("编号", typeof(String));
            dt1.Columns.Add("feeNo", typeof(String));
            dt1.Columns.Add("feeAmount", typeof(String));
            dt1.Columns.Add("remark", typeof(String));
         
            //默认5行
            DataRow dr1 = dt1.NewRow();
            DataRow dr2 = dt1.NewRow();
            DataRow dr3 = dt1.NewRow();
            DataRow dr4 = dt1.NewRow();
            DataRow dr5 = dt1.NewRow();
            dr1[1] = "0";
            dr1[2] = "";
            dr1[3] = "";
           

            dr2[1] = "0";
            dr2[2] = "";
            dr2[3] = "";
          

            dr3[1] = "0";
            dr3[2] = "";
            dr3[3] = "";
           

            dr4[1] = "0";
            dr4[2] = "";
            dr4[3] = "";
           

            dr5[1] = "0";
            dr5[2] = "";
            dr5[3] = "";
          
            dt1.Rows.Add(dr1);
            dt1.Rows.Add(dr2);
            dt1.Rows.Add(dr3);
            dt1.Rows.Add(dr4);
            dt1.Rows.Add(dr5);
            dt1.AcceptChanges();
            gvFeeTemplatedetail.DataSource = dt1;
            gvFeeTemplatedetail.DataBind();
 
        }

        /// <summary>
        /// 根据模板查看页面的内容重新绑定数据库
        /// </summary>
        public void ReBind()
        {
            ddlTypeID.SelectedValue = Session["typeID"].ToString();
            ddlGoodsName.SelectedValue = Session["goodsType"].ToString();
            tbTemplateName.Text = Session["templetName"].ToString();
            gvFeeTemplatedetail.DataSource = GetTemplate();
            gvFeeTemplatedetail.DataBind();
        }

        /// <summary>
        /// 获取模板的详细内容
        /// </summary>
        /// <returns></returns>
        public DataTable GetTemplate()
        {
            DataTable dt = new DataTable();
            DataSet ds = new DataSet();
            Fee_templetdetailManage ftdm = new Fee_templetdetailManage();
            ds = ftdm.GetList("feetemplet_ID=" + int.Parse(Session["feetemplet_ID"].ToString()) + " ");
            dt = ds.Tables[0];
            return dt;

        }



      

        /// <summary>
        /// 添加一行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAddRow_Click(object sender, EventArgs e)
        {
            DataTable dt = GetDataFromGrid();
            DataRow dr = dt.NewRow();
          
            dr[1] = "0";
            dr[2] = "";
            dr[3] = "";
            
            dt.Rows.Add(dr);
            dt.AcceptChanges();
            gvFeeTemplatedetail.DataSource = dt;
            gvFeeTemplatedetail.DataBind();
        }

        /// <summary>
        /// 获取已填入的数据
        /// </summary>
        /// <returns></returns>
        protected DataTable GetDataFromGrid()
        {
            DataTable dt1 = new DataTable("Table1");
            dt1.Columns.Add("编号");
            dt1.Columns.Add("feeNo");
            dt1.Columns.Add("feeAmount");
            dt1.Columns.Add("remark");
          
         
            for (int i = 0; i < gvFeeTemplatedetail.Rows.Count; i++)
            {
                GridViewRow gRow = gvFeeTemplatedetail.Rows[i];
                DataRow newRow = dt1.NewRow();
                newRow[0] = i + 1;
                newRow[1] = ((DropDownList)gRow.FindControl("ddlfeeTypeID")).SelectedValue;
                newRow[2] = ((TextBox)gRow.FindControl("tbfeeAmount")).Text;
                newRow[3] = ((TextBox)gRow.FindControl("tbremark")).Text;
              
                dt1.Rows.Add(newRow);


            }
            dt1.AcceptChanges();

            return dt1;
        }

        protected void gvFeeTemplatedetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }

        /// <summary>
        /// 保存模板
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            //验证
            if (tbTemplateName.Text.Trim().Length < 1)
            { MessageBox.Show(this ,"请填写模板名称"); }
            else if (ddlGoodsName.SelectedValue == "0")
            { MessageBox.Show(this, "请选择货物类别"); }
            else
            { SaveData(); }
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        public void SaveData()
        {  //验证费用是否同类
            for (int j = 0; j < gvFeeTemplatedetail.Rows.Count - 1; j++)
            {
                for (int n = 1; n < gvFeeTemplatedetail.Rows.Count; n++)
                {
                    if (((DropDownList)gvFeeTemplatedetail.Rows[j].Cells[1].FindControl("ddlfeeTypeID")).SelectedValue == ((DropDownList)gvFeeTemplatedetail.Rows[n].Cells[1].FindControl("ddlfeeTypeID")).SelectedValue && ((DropDownList)gvFeeTemplatedetail.Rows[j].Cells[1].FindControl("ddlfeeTypeID")).SelectedValue != "0" && n != j)
                    {
                        goto end;
                    }
                }
            }

            CustomsSystem.Model.Fee_templetdetail Fee_templetdetail = new CustomsSystem.Model.Fee_templetdetail();
            Fee_templetdetailManage ftdm = new Fee_templetdetailManage();
            if (Session["feetemplet_ID"] != null && Session["feetemplet_ID"].ToString() != "")
            {
                ftdm.Delete(int.Parse(Session["feetemplet_ID"].ToString()));//删掉以前的数据
            }


            //下面是保存gv的数据
            Fee_templetManage ftm = new Fee_templetManage();
            CustomsSystem.Model.Fee_templet ft = new CustomsSystem.Model.Fee_templet();
            ft.templetName = tbTemplateName.Text.Trim();
            ft.typeID = int.Parse(ddlTypeID.SelectedValue);
            ft.goodsType = ddlGoodsName.SelectedValue;

            // ft.infoWKTypeID 
            //ft.remark 
            //更新或者是添加新模板
            if (Session["feetemplet_ID"] != null && Session["feetemplet_ID"].ToString() != "")
            {
                ft.ID = int.Parse(Session["feetemplet_ID"].ToString());
                ftm.Update(ft);
                Fee_templetdetail.feetemplet_ID = int.Parse(Session["feetemplet_ID"].ToString());
            }
            else
            {
                ftm.Add(ft);
                CustomsSystem.Model.Fee_templet newft = new CustomsSystem.Model.Fee_templet();
                newft = ftm.GetModel(" templetName='" + ft.templetName + "' and typeID=" + ft.typeID + " and goodsType=" + ft.goodsType + " ");
                Fee_templetdetail.feetemplet_ID = newft.ID;
            }//添加模板

            //保存模板具体内容
            string fa;
            for (int i = 0; i < gvFeeTemplatedetail.Rows.Count; i++)
            {


                Fee_templetdetail.feeNo = ((DropDownList)gvFeeTemplatedetail.Rows[i].Cells[1].FindControl("ddlfeeTypeID")).SelectedValue;
                fa = ((TextBox)gvFeeTemplatedetail.Rows[i].Cells[2].FindControl("tbfeeAmount")).Text.Trim();
                if (fa == "" || fa == null)
                { Fee_templetdetail.feeamount = null ; }
                else  Fee_templetdetail.feeamount = decimal.Parse(fa);
                Fee_templetdetail.remark = ((TextBox)gvFeeTemplatedetail.Rows[i].Cells[3].FindControl("tbremark")).Text.Trim();
                if (Fee_templetdetail.feeNo != "0")
                { ftdm.Add(Fee_templetdetail); }

            }
            MessageBox.ShowAndRedirect(this, "保存成功！", "feeTemplateDesign.aspx");

        end: MessageBox.Show(this, "费用重复，请修改费用类别！");
        }
        /// <summary>
        /// 获取货物类别
        /// </summary>
        /// <returns></returns>
        public DataTable GetGoodsType()
        {
            CodeManage codeManage = new CodeManage();
            DataSet ds = codeManage.GetList(" codeType='goodType'");
            DataTable dt = ds.Tables[0];
            return dt;

        }

        /// <summary>
        /// 获取费用类别
        /// </summary>
        /// <returns></returns>
        public DataTable GetFeeType()
        {
            FeeTypeManage feeTypeManage = new FeeTypeManage();
            DataSet ds = feeTypeManage.GetFeeList(" feeType =0 ");
            DataTable dt = ds.Tables[0];
            return dt;

        }
        

    }
}